package com.fly.system.controller;

import com.fly.common.model.SysUserModel;
import com.fly.common.model.ZtreeModel;
import com.fly.common.msg.ResultMsg;
import com.fly.system.model.SysRoleMenuModel;
import com.fly.system.model.SysRoleModel;
import com.fly.system.model.SysUserRoleModel;
import com.fly.system.service.SysMenuService;
import com.fly.system.service.SysRoleService;
import com.fly.system.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @CLASSNAME: SysMenuController
 * @author: Fly
 * 日期: 2020/6/13  17:26
 * 描述:角色管理
 **/
@Controller
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    /* * 
     *@author Fuly
     * 日期：2020/7/18 11:56
     * 描述：
     */
    @RequestMapping("/index")
    @RequiresPermissions("sys:user:view")
    public String toList(){
        return "/system/user/user_list";
    }

    /* * 
     *@author Fuly
     * 
     * 日期：2020/7/18 11:56
     * 
     * 描述：
     *
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:user:add")
    public String add(Model model){
        model.addAttribute("roleAll",sysUserService.findRoleAll());
        return "/system/user/add";
    }
    /* *
     *@author Fuly
     * 
     * 日期：2020/7/18 11:56
     * 
     * 描述：
     *
     */
    @PostMapping("/add/save")
    @ResponseBody
    public ResultMsg addSave(SysUserModel model){
        int i = sysUserService.saveModel(model);
        if(i > 0){
            return ResultMsg.success();
        } else {
            return ResultMsg.error();
        }
    }

    /* * 
     *@author Fuly
     * 
     * 日期：2020/7/18 11:56
     * 
     * 描述：
     *
     */
    @RequestMapping("/findPage")
    @ResponseBody
    public List<SysUserModel> findPage(SysUserModel model){
        return sysUserService.findPage(model);
    }

    /* * 
     *@author Fuly
     * 
     * 日期：2020/7/18 11:56
     * 
     * 描述：
     *
     */
    @RequestMapping("/edit/{id}")
    @RequiresPermissions("sys:user:edit")
    public String edit(@PathVariable Long id, Model m){
        SysUserModel userModel = sysUserService.findModelById(id);
        List<SysUserRoleModel> list = sysUserService.findUserRoleListByUserId(userModel.getId());
        m.addAttribute("user",userModel);
        m.addAttribute("userRoleList",list);
        m.addAttribute("roleAll",sysUserService.findRoleAll());
        return "/system/user/edit";
    }

    @RequestMapping("/edit/save")
    @ResponseBody
    public ResultMsg editSave(SysUserModel model){
        int i = sysUserService.editModelById(model);
        if(i > 0){
            return ResultMsg.success();
        } else {
            return ResultMsg.error();
        }
    }

    /* * 
     *@author Fuly
     * 
     * 日期：2020/7/18 11:56
     * 
     * 描述：
     *
     */
    @RequestMapping("/remove")
    @ResponseBody
    @RequiresPermissions("sys:user:edit")
    public ResultMsg remove(SysUserModel model){
        int i = sysUserService.removeModelById(model);
        if(i > 0){
            return ResultMsg.success();
        } else {
            return ResultMsg.error();
        }
    }
}
